package com.platform.task;

import com.platform.base.domain.CollectorInfo;
import com.platform.base.service.ICollectorInfoService;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.rabbit.RabbitUtils;
import com.platform.common.utils.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 采集器定时调度任务
 *
 * @author platform
 */
@Component("collectorTask")
@Slf4j
public class CollectorTask {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ICollectorInfoService collectorInfoService;

    @Autowired
    private RabbitUtils rabbitUtils;

    /**
     * 根据采集器id采集数据
     * @param collectorIds
     */
    public void collectorTaskById(String collectorIds) {
        for(String collectorId : collectorIds.split(",")){
            //获取采集器高位
            String cenhigh = collectorId.substring(0,2);
            //获取采集器低位
            String cenlow = collectorId.substring(2,4);
            //判断通道是否存在
            if(!redisUtil.hHasKey("sys-netty", collectorId)){
                log.error("采集器" + collectorId + "已离线");

                continue;
            }
            //控制码
            String cont = "05";
            //计算校验码
            int ll = NettyUtil.get10HexNum(cenhigh)+NettyUtil.get10HexNum(cenlow)+NettyUtil.get10HexNum(cont)+NettyUtil.get10HexNum("09");
            String check = NettyUtil.get16HexNum(ll);
            if(check.length()<2){
                check = "0" + check;
            }else{
                check = check.substring(check.length()-2);
            }
            log.info("采集器"+collectorId+"执行定时采集任务");
            String response = "AA "+ cenlow +" "+ cenhigh + " 00 00 00 00 " + cont + " 00 00 00 00 00 00 " + check + " FF ";
            String revertStr = response.replace(" ", "");
            rabbitUtils.writeToClient(collectorId, revertStr, collectorId+"全部设备信息采集");
        }
    }

    /**
     * 采集所有采集器信息
     * @param
     */
    public void collectorTask() {
        Map map = redisUtil.hmget("sys-netty");
        for(Object key : map.keySet()){
            String collectorId = key.toString();
            //获取采集器高位
            String cenhigh = collectorId.substring(0,2);
            //获取采集器低位
            String cenlow = collectorId.substring(2,4);
            //控制码
            String cont = "05";
            //计算校验码
            int ll = NettyUtil.get10HexNum(cenhigh)+NettyUtil.get10HexNum(cenlow)+NettyUtil.get10HexNum(cont);
            String check = NettyUtil.get16HexNum(ll);
            if(check.length()<2){
                check = "0" + check;
            }else{
                check = check.substring(check.length()-2);
            }
            log.info("采集器"+ collectorId +"执行定时采集任务");
            String response = "AA "+ cenlow +" "+ cenhigh + " 00 00 00 00 " + cont + " 00 00 00 00 00 00 " + check + " FF ";
            String revertStr = response.replace(" ", "");
            rabbitUtils.writeToClient(collectorId, revertStr, collectorId+"全部设备信息采集");
        }
    }
    /**
     * 采集所有采集器Valve信息
     * @param
     */
    public void collectorTaskValve() {
        Map map = redisUtil.hmget("sys-netty");
        for(Object key : map.keySet()){
            String collectorId = key.toString();
            //获取采集器高位
            String cenhigh = collectorId.substring(0,2);
            //获取采集器低位
            String cenlow = collectorId.substring(2,4);
            //控制码
            String cont = "16";
            //计算校验码
            int ll = NettyUtil.get10HexNum(cenhigh)+NettyUtil.get10HexNum(cenlow)+NettyUtil.get10HexNum(cont);
            String check = NettyUtil.get16HexNum(ll);
            if(check.length()<2){
                check = "0" + check;
            }else{
                check = check.substring(check.length()-2);
            }
            log.info("采集器"+ collectorId +"执行阀门定时采集任务");
            String response = "AA "+ cenlow +" "+ cenhigh + " 00 00 00 00 " + cont + " 00 00 00 00 00 00 " + check + " FF ";
            String revertStr = response.replace(" ", "");
            rabbitUtils.writeToClient(collectorId, revertStr, collectorId+"全部阀门信息采集");
        }
    }

    /**
     * 采集所有采集器Meter信息
     * @param
     */
    public void collectorTaskMeter() {
        Map map = redisUtil.hmget("sys-netty");
        for(Object key : map.keySet()){
            String collectorId = key.toString();
            //获取采集器高位
            String cenhigh = collectorId.substring(0,2);
            //获取采集器低位
            String cenlow = collectorId.substring(2,4);
            //控制码
            String cont = "17";
            //计算校验码
            int ll = NettyUtil.get10HexNum(cenhigh)+NettyUtil.get10HexNum(cenlow)+NettyUtil.get10HexNum(cont);
            String check = NettyUtil.get16HexNum(ll);
            if(check.length()<2){
                check = "0" + check;
            }else{
                check = check.substring(check.length()-2);
            }
            log.info("采集器"+ collectorId +"执行热表定时采集任务");
            String response = "AA "+ cenlow +" "+ cenhigh + " 00 00 00 00 " + cont + " 00 00 00 00 00 00 " + check + " FF ";
            String revertStr = response.replace(" ", "");
            rabbitUtils.writeToClient(collectorId, revertStr, collectorId+"全部热表信息采集");
        }
    }
}
